package com.toefl.speaking.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.Button;

import com.toefl.speaking.R;

public class MainActivity extends ActivityGroup {

	private static ViewGroup container;
	private static MainActivity thiz;
	private static Map<Integer, View> subViews = new HashMap<Integer, View>();

	private Button buttonCalDate, buttonHealthNotice, buttonMore, buttonPostQuestion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);
		MainActivity.subViews.clear();
		// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		container = (ViewGroup) this.findViewById(R.id.container);
		buttonCalDate = (Button) findViewById(R.id.buttonCalDate);
		buttonHealthNotice = (Button) findViewById(R.id.buttonHealthNotice);
		buttonMore = (Button) findViewById(R.id.buttonMore);
		buttonPostQuestion = (Button) findViewById(R.id.buttonPostQuestion);
		NavigationButtonsOnClickListener listener = new NavigationButtonsOnClickListener();
		buttonHealthNotice.setOnClickListener(listener);
		buttonCalDate.setOnClickListener(listener);
		buttonMore.setOnClickListener(listener);
		buttonPostQuestion.setOnClickListener(listener);
		MainActivity.thiz = this;
		switchActivity(HomeActivity.class);
	}

	private class NavigationButtonsOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.buttonCalDate:
				switchActivity(HomeActivity.class);
				break;
			case R.id.buttonHealthNotice:
				switchActivity(AudioActivity.class);
				break;
			case R.id.buttonMore:
				switchActivity(MoreActivity.class);
				break;
			case R.id.buttonPostQuestion:
				switchActivity(testCommuniteActivity.class);
				break;
			}
		}
	}

	/**
	 * ��ת����Ӧ��activity��ʹ�û��档
	 * 
	 * @param c Actvity.class
	 */
	public static void switchActivity(Class<?> c) {
		switchActivity(c, false, null);
	}

	/**
	 * ��ת����Ӧ��activity���Զ����Ƿ�ʹ�û��档
	 * 
	 * @param c Actvity.class
	 * @param reload �Ƿ�ʹ�û���
	 */
	public static void switchActivity(Class<?> c, boolean reload) {
		switchActivity(c, reload, null);
	}

	/**
	 * ��ת����Ӧ��activity�����ṩintent��ʹ�û��档
	 * 
	 * @param c Actvity.class
	 * @param intent
	 */
	public static void switchActivity(Class<?> c, Intent intent) {
		switchActivity(c, false, intent);
	}

	/**
	 * ��ת����Ӧ��activity���Զ����Ƿ�ʹ�û��档�Զ���intent��intent��Ϊ�ա�
	 * 
	 * @param c Actvity.class
	 * @param reload
	 * @param intent
	 */
	public static void switchActivity(Class<?> c, boolean reload, Intent intent) {
		// ���ʹ�û���ɹ�����ֱ�ӷ��ء�
		if (!reload && useCachedActivity(c)) {
			return;
		}
		createAndAddActivityToContainer(c, intent);
	}

	private static void createAndAddActivityToContainer(Class<?> c, Intent intent) {
		if (intent == null) {
			intent = new Intent(thiz, c);
		} else {
			intent.setClass(thiz, c);
		}
		int activityKey = c.hashCode();
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		View v = thiz.getLocalActivityManager().startActivity(String.valueOf(activityKey), intent).getDecorView();
		v.setId(activityKey);
		subViews.remove(activityKey);
		subViews.put(activityKey, v);
		container.removeAllViews();
		container.addView(v);
	}

	private static boolean useCachedActivity(Class<?> c) {
		if (isActivityShown(c)) {
			return true;
		}
		container.removeAllViews();
		container.startAnimation(AnimationUtils.loadAnimation(thiz, android.R.anim.fade_in));
		View view = subViews.get(c.hashCode());
		if (view == null) {
			return false;
		}
		ViewParent parent = view.getParent();
		if (parent == null) {
			container.removeAllViews();
			container.addView(view);
			return true;
		}
		subViews.remove(c.hashCode());
		return false;
	}

	private static boolean isActivityShown(Class<?> c) {
		View child = container.getChildAt(0);
		if (child != null && c.hashCode() == child.getId()) {
			return true;
		}
		return false;
	}

}
